/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Configuration;
using System.Reflection;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{

  [Image(typeof(ObjectTypeDefNode))]
  [SelectedImage(typeof(ObjectTypeDefNode))]
  public class ObjectMappingNode : ConfigurationNode
  {
    private ObjectTypeDefNode typeNode;
    private ObjectMappingTableNode primaryTableNode;
    private EventHandler<ConfigurationNodeChangedEventArgs> typeNodeRemovedHandler;
    private EventHandler<ConfigurationNodeChangedEventArgs> primaryTableNodeRemovedHandler;

    public ObjectMappingNode()
      : this(new ObjectMappingData(Resource.DefaultObjectTypeDefName))
    {
        
    }

    public ObjectMappingNode(ObjectMappingData data)
      : base((data == null) ? Resource.DefaultObjectTypeDefName : data.Name)
    {
      if (data == null) { throw new ArgumentNullException("ObjectMappingData"); }
      this.typeNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnTypeNodeRemoved);
      this.primaryTableNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnPrimaryTableNodeRemoved);

      if (data.Tables != null)
      {
        foreach (ObjectTableData table in data.Tables)
        {
          ObjectMappingTableNode tNode = new ObjectMappingTableNode(table);
          this.AddNode(tNode);
          if (data.PrimaryTable == table.Name)
          {
            this.PrimaryTable = tNode;
          }
        }
        if (string.IsNullOrEmpty(data.PrimaryTable) && this.Nodes != null && this.Nodes.Count > 0)
        {
          this.PrimaryTable = (ObjectMappingTableNode)this.Nodes[0];
        }
      }
    }		

    [Browsable(false)]
    public ObjectMappingData MappingData
    {
      get
      {
        ObjectMappingData data = new ObjectMappingData(Name);
        if (this.primaryTableNode != null)
        {
          data.PrimaryTable = this.primaryTableNode.ObjectTableData.Name;
        }
        if (this.Nodes != null)
        {
          foreach (ObjectMappingTableNode node in this.Nodes)
          {
            data.Tables.Add(node.ObjectTableData);
          }
        }
	      return data;
      }
    }

    [Required]
    [SRDescription("ObjectTypeReferenceDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(ObjectTypeDefNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public ObjectTypeDefNode Type
    {
      get { return typeNode; }
      set
      {
        typeNode = LinkNodeHelper.CreateReference<ObjectTypeDefNode>(typeNode,
                                                                    value,
                                                                    typeNodeRemovedHandler,
                                                                    null);
        this.Name = typeNode.Name;
      }
    }
    [Required]
    [SRDescription("ObjectTableReferenceDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(ObjectMappingTableNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public ObjectMappingTableNode PrimaryTable
    {
      get { return primaryTableNode; }
      set
      {
        primaryTableNode = LinkNodeHelper.CreateReference<ObjectMappingTableNode>(primaryTableNode,
                                                                    value,
                                                                    primaryTableNodeRemovedHandler,
                                                                    null);
      }
    }
    protected override void Dispose(bool disposing)
    {
      try
      {
        if (disposing)
        {
          if (null != typeNode)
          {
            typeNode.Removed -= typeNodeRemovedHandler;
          }
          if (null != primaryTableNode)
          {
            primaryTableNode.Removed -= primaryTableNodeRemovedHandler;
          }
        }
      }
      finally
      {
        base.Dispose(disposing);
      }
    }

    protected override void OnChildAdded(ConfigurationNodeChangedEventArgs e)
    {
      base.OnChildAdded(e);
      ObjectMappingTableNode node = e.Node as ObjectMappingTableNode;
      Debug.Assert(null != node, "Only ObjectMappingTableNode objects can be added to a ObjectMappingNode");

    }
    private void OnTypeNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
      this.typeNode = null;
    }
    private void OnPrimaryTableNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
      this.primaryTableNode = null;
    }

  }
}